package com.shangxia.monomer.study.designPatterns.behavioralType.ChainOfResponsibility;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: monomer
 * @ClassName: ChainDemo
 * @Time: 2021/7/29
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class ChainDemo {
    public static void main(String[] args) {
        Request request = Request.builder().frequent(true).login(true).authority(true).build();
        RequestHandle handle = new LoginRequestHandle(new FrequentRequestHandle(new AuthorityRequestHandle()));
        RequestHandle handle2 = new FrequentRequestHandle(new LoginRequestHandle());
        System.out.println(handle.success(request));
        System.out.println();
        System.out.println(handle2.success(request));
    }
}

@Data
@Builder
class Request {
    private Boolean login;
    private Boolean frequent;
    private Boolean authority;
}

@NoArgsConstructor
@AllArgsConstructor
abstract class RequestHandle {
    public RequestHandle next;

    public abstract Boolean success(Request request);
}


class LoginRequestHandle extends RequestHandle {
    public LoginRequestHandle() {
        super();
    }

    public LoginRequestHandle(RequestHandle next) {
        super(next);
    }

    @Override
    public Boolean success(Request request) {
        System.out.println("LoginRequestHandle");
        if (request.getLogin()) {
            if (null == next) {
                return true;
            } else {
                return next.success(request);
            }
        }
        return false;
    }
}

@AllArgsConstructor
class FrequentRequestHandle extends RequestHandle {

    public FrequentRequestHandle(RequestHandle next) {
        super(next);
    }

    @Override
    public Boolean success(Request request) {
        System.out.println("FrequentRequestHandle ");
        if (request.getFrequent()) {
            if (null == next) {
                return true;
            }
            return next.success(request);
        }
        return false;
    }
}

@AllArgsConstructor
class AuthorityRequestHandle extends RequestHandle {

    public AuthorityRequestHandle(RequestHandle next) {
        super(next);
    }

    @Override
    public Boolean success(Request request) {
        System.out.println("AuthorityRequestHandle");
        if (request.getAuthority()) {
            if (null == next) {
                return true;
            }
            return next.success(request);
        }
        return false;
    }
}